package com.example.mydata.Controller;

import com.example.mydata.service.LibraryService;

import com.example.mydata.Exception.LibraryException;
import com.example.mydata.Vo.Book;
import com.example.mydata.Vo.User;
import com.example.mydata.service.LibraryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/library")
@Api(tags = "图书馆管理系统 API")
public class UserController {

    @Autowired
    private LibraryService libraryService;

    @PostMapping("/borrow")
    @ApiOperation(value = "借书", notes = "根据用户 ID和书籍 ID 借书")
    @ApiImplicitParams({   //@ApiImplicitParams用于定义隐式参数（即通过查询字符串或表单提交的参数）。
            @ApiImplicitParam(name = "bookId", value = "书籍 ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String")
    })

    //这些是方法的输入参数，通过@RequestParam注解从HTTP请求中提取。
    public ResponseEntity<String> borrowBook(@RequestParam("userId") String userId, @RequestParam("name") String name, @RequestParam("bookId") String bookId) {
        User user = new User();
        user.setUserId(userId);
        try {
            Book book = libraryService.selectBookById(bookId);
            if (book == null) {
                return ResponseEntity.badRequest().body("书籍未找到");
            }
            if (libraryService.borrowBook(user, book)) {
                return ResponseEntity.ok("借书成功");
            } else {
                return ResponseEntity.badRequest().body("借书失败");
            }
        } catch (LibraryException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @PostMapping("/return")
    @ApiOperation(value = "还书", notes = "根据用户 ID和书籍 ID 还书")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户 ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "bookId", value = "书籍 ID", required = true, dataType = "String", paramType = "query")
    })
    public ResponseEntity<String> returnBook(@RequestParam("userId") String userId, @RequestParam String bookId) {
        User user = new User();
        user.setUserId(userId);
        try {
            Book book = libraryService.selectBookById(bookId);
            if (book == null) {
                return ResponseEntity.badRequest().body("书籍未找到");
            }
            int result = libraryService.returnBook(user, book);
            if (result > 0) {
                return ResponseEntity.ok("还书成功");
            } else {
                return ResponseEntity.badRequest().body("还书失败");
            }
        } catch (LibraryException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/book/{id}")
    @ApiOperation(value = "根据 ID 查询书籍", notes = "通过书籍 ID 查询书籍详情")
    @ApiImplicitParam(name = "id", value = "书籍 ID", required = true, dataType = "String", paramType = "path")
    public ResponseEntity<Book> getBookById(@PathVariable String id) {
        Book book = libraryService.selectBookById(id);
        if (book == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(book);
    }
}